"ui";
let url = "https://h5.m.jd.com/babelDiy/Zeus/3KSjXqQabiTuD1cJ28QskrpWoBKT/index.html?babelChannel=45&collectionId=519&lng=113.39183&lat=23.13042&sid=ae12c4c53cdb18ded15e85ad8547e2aw&un_area=19_1601_3633_0";
ui.layout(
    <frame w="*" h="*">
        <webview id="webView" layout_below="title" src="{{ url }}"/>
    </frame>
);

// 这里外部文本资源，必须以非js类型，否则，打包时会被加密，导致乱码
let outerJsFile = ["vsConsole.txt", "tree.txt"];
ui.webView.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
    onPageFinished: (webView, curUrl)=>{
        if (curUrl === url) {
            toastLog('页面加载完成')
            ui.run(() => {
                outerJsFile.forEach((file, index) => {
                    try {
                        let js = files.read(file);
                        ui.webView.loadUrl("javascript:" + js);
                        console.log("注入脚本: " + file);
                    } catch (e) {
                        toastLog("注入脚本失败: " + file);
                        console.error(e);
                    }
                });
            })
        } else {
            toastLog("返回");
            webView.goBack();
        }
    }
});
ui.webView.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, {
    onConsoleMessage: (msg) => {
        console.log("[%s:%s]: %s", msg.sourceId(), msg.lineNumber(), msg.message());
    }
});
ui.webView.setOnLongClickListener(new JavaAdapter(android.view.View.OnLongClickListener, {
    onLongClick: (view) => {
        toastLog("你按扁我了 🐖");
        return true;
    }
}));
/** 返回键停止js */
ui.emitter.on("back_pressed", e => {
    ui.webView.destroy();
});

ui.webView.loadUrl(url);